package com.app.ui.view.list.head;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Message;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.app.utiles.other.DLog;
import com.wxj.zx.R;


//聊天界面的headView
public class HeaderViewRefreshChat extends BaseRefreshLayout {
    private View container;
    private int headerViewHeight; // 头部View的高
    //刷新动画阈值
    private int refreshHeight;
    private TextView chatMsgTv;
    private ProgressBar refreshProgressbar;

    public HeaderViewRefreshChat(Context context) {
        super(context);
        initView(context);
    }

    /**
     * @param context
     * @param attrs
     */
    public HeaderViewRefreshChat(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        headerViewHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60,
                getContext().getResources().getDisplayMetrics());
        refreshHeight = headerViewHeight;
        LayoutParams lp = new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, headerViewHeight);
        container = LayoutInflater.from(context).inflate(
                R.layout.item_refresh_list_header_chat, null);
        addView(container, lp);
        // 找到头部页面里的控件
        chatMsgTv = (TextView) findViewById(R.id.chat_msg_tv);
        refreshProgressbar = (ProgressBar) findViewById(R.id.chat_progressbar);
        chatMsgTv.setText("加载历史消息");
        setPadding(0, -1 * headerViewHeight, 0, 0);
        postInvalidate();
    }

    @Override
    public int getVisiableHeight() {
        int top = getPaddingTop();
        int visiableHeight = headerViewHeight + top;
        return visiableHeight;
    }


    private void setRefresh(int state) {
        //正在刷新中
        switch (state) {
            case 1:
                chatMsgTv.setText("加载中...");
                refreshProgressbar.setVisibility(View.VISIBLE);
                break;
            case 2:
                chatMsgTv.setText("加载完成");
                refreshProgressbar.setVisibility(View.INVISIBLE);
                break;
            case 3:
                chatMsgTv.setText("下拉加载消息");
                refreshProgressbar.setVisibility(View.GONE);
                break;

        }


    }

    //=========================下拉刷新=======================================
    @Override
    public void onTouchEvent(float y) {
        if (y < 0) {
            y = 0;
        }
        if (y > headerViewHeight) {
            y = headerViewHeight;
        }
        float heightHide = headerViewHeight - y;
        DLog.e("下拉刷新", "==========" + y);
        setPadding(0, (int) -heightHide, 0, 0);
        postInvalidate();
        //刷新中
        if (isRefresh) {
            return;
        }
        //箭头动画
        int topHeight = getPaddingTop();
        int HeightShow = headerViewHeight + topHeight;
        if (HeightShow >= refreshHeight) {
            chatMsgTv.setText("松开加载消息");
        }
        if (HeightShow < refreshHeight) {
            chatMsgTv.setText("下拉加载消息");
        }
    }

    @Override
    public boolean onTouchEventStop() {
        int top = getPaddingTop();
        int h = headerViewHeight - Math.abs(top);
        boolean isRefreshTemp = h >= refreshHeight;
        if (isRefreshTemp) {
            h -= refreshHeight;
        }
        if (isRefresh) {
            isRefreshTemp = false;
        }
        if (!isRefresh && isRefreshTemp) {
            isRefresh = true;
            setRefresh(1);
        }
        animator(h);
        return isRefreshTemp;
    }

    @Override
    public void setRefreshComplete() {
        if (!isRefresh) {
            return;
        }
        setRefresh(2);
        handlerRefresh.sendEmptyMessageDelayed(1, 500);

    }

    @Override
    protected void handleMessage(Message msg) {
        //刷新完成回滚
        isRefresh = false;
        int top = getPaddingTop();
        int h = headerViewHeight - Math.abs(top);
        animator(h);
    }

    private int paddingTop;

    //开始回滚动画
    private void animator(int heightBack) {
        DLog.e("动画回滚距离", heightBack);
        paddingTop = getPaddingTop();
        startAnimatorView(heightBack, 200);
    }

    //=========================动画监听=======================================
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {

        float move = (Float) animation.getAnimatedValue();
        int height = (int) move;
        int top = paddingTop - height;
        if (top < -headerViewHeight) {
            top = -headerViewHeight;
        }
        // DLog.e("动画回滚", "height" + height + " top" + top + " headerViewHeight" + headerViewHeight);
        setPadding(0, top, 0, 0);
        postInvalidate();
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        if (isRefresh) {
            return;
        }
        setPadding(0, -headerViewHeight, 0, 0);
        setRefresh(3);
        invalidate();
    }
}
